﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.GlobalClass;
using Zocono.WCS.Domain.PlcBytesObject;
using Zocono.WCS.Domain.ProcessModuleDomain;

namespace Zocono.WCS.Domain.EquipmentProcessDomain
{
    public class ProcessPlanarService : IProcessPlanarService
    {
        private readonly ILogUtil _logUtil;
        private readonly IPlanarMessageServiceFactory _planarEquipmentMessageServiceFactory;
        private readonly IPlanarProcessServiceMapInfoRepository _planarProcessServiceMapInfoRepository;
        private readonly IBytesMapObjectInfoRepository _bytesMapObjectInfoRepository;

        public ProcessPlanarService(IPlanarMessageServiceFactory planarEquipmentMessageServiceFactory, ILogUtil logUtil, IPlanarProcessServiceMapInfoRepository planarProcessServiceMapInfoRepository, IBytesMapObjectInfoRepository bytesMapObjectInfoRepository)
        {
            _planarEquipmentMessageServiceFactory = planarEquipmentMessageServiceFactory;
            _logUtil = logUtil;
            _planarProcessServiceMapInfoRepository = planarProcessServiceMapInfoRepository;
            _bytesMapObjectInfoRepository = bytesMapObjectInfoRepository;
        }

        /// <summary>
        /// 处理平面设备的报文
        /// </summary>
        /// <param name="planarEquipmentDbObject">平面设备报文</param>
        /// <param name="plcStateProcessModule">Plc设备在线离线状态报文</param>
        /// <returns></returns>
        public async Task ProcessAsync(EquipmentDbObject planarEquipmentDbObject/*, ProcessModule plcStateProcessModule*/)
        {
            ////1、创建状态服务，更新设备状态
            ////2、按PLC动作报文创建动作报文服务，执行报文处理业务。
            //BytesObjectBase plcState = null;
            ////1、状态报文服务
            //var statePlcWcsMessages = planarEquipmentDbObject.bytesObjectBases.FindAll(o => o.ObjectType.Contains("ActionBO"));

            //if (statePlcWcsMessages != null && statePlcWcsMessages.Count > 0 /*&& plcStateProcessModule != null*/)
            //{
            //    long plcConnectionID = statePlcWcsMessages[0].PlcConnectionID;
            //    //plcState = plcStateProcessModule.equipmentDbObjects[0].bytesObjectBases.Find(o => o.PlcConnectionID == plcConnectionID);

            //    foreach (var stateMessage in statePlcWcsMessages)
            //    {
            //        var stateMessageService = _planarEquipmentMessageServiceFactory.CreateStateService(stateMessage.ObjectType);
            //        await stateMessageService.ProcessStateMessageAsync(stateMessage/*, plcState*/);
            //    }
            //}

            //2、动作报文服务
            var actionMessages = planarEquipmentDbObject.bytesObjectBases.FindAll(o => o.ObjectType.Contains("PlcActionBO"));
            //var actionMessages = planarEquipmentDbObject.bytesObjectBases.FindAll(o => o.ObjectType.Contains("ActionBO"));


            if (actionMessages != null)
            {
                foreach (var actionMessage in actionMessages)
                {
                    long plcConnectionID = actionMessage.PlcConnectionID;
                    //plcState = plcStateProcessModule.equipmentDbObjects[0].bytesObjectBases.Find(o => o.PlcConnectionID == plcConnectionID);

                    //区分平面报文类
                    var actionMessageService = _planarEquipmentMessageServiceFactory.CreateActionService(actionMessage.ObjectType);
                    await actionMessageService.ProcessActionMessageAsync(actionMessage, planarEquipmentDbObject/*, plcState*/);
                }
            }


        }

        /// <summary>
        /// 定时加载数据
        /// </summary>
        /// <returns></returns>
        public async Task ReloadPlanarActionMessage()
        {
            GlobalResource.PlanarProcessServiceMapInfos = await _planarProcessServiceMapInfoRepository.GetListAsync();
            GlobalResource.BytesMapObjectInfos = await _bytesMapObjectInfoRepository.GetListAsync();
        }

    }
}
